home *** CD-ROM | disk | FTP | other *** search
Text File | 2000-09-28 | 19.1 KB | 681 lines | [TEXT/CWIE] |
- /*
- File: DSp Context Switch Main.c
-
- Contains: Source file for demonstration of DSp 1.7 or 1.1.4 context switching
- right arrow for a larger context; left arrow for smaller; Cmd-Q to exit.
-
- Written by: Geoff Stahl (ggs)
-
- Copyright: Copyright (c) 1999 Apple Computer, Inc., All Rights Reserved.
-
- Change History (most recent first):
-
- <3+> 10/11/99 ggs
- <3> 10/10/99 ggs Header clean up
- <2> 10/10/99 ggs Added support for DSP 1.1.4 with code that does the equivalent
- of context switching
- <1> 10/10/99 ggs Initial Add
-
- Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
- ("Apple") in consideration of your agreement to the following terms, and your
- use, installation, modification or redistribution of this Apple software
- constitutes acceptance of these terms. If you do not agree with these terms,
- please do not use, install, modify or redistribute this Apple software.
-
- In consideration of your agreement to abide by the following terms, and subject
- to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
- copyrights in this original Apple software (the "Apple Software"), to use,
- reproduce, modify and redistribute the Apple Software, with or without
- modifications, in source and/or binary forms; provided that if you redistribute
- the Apple Software in its entirety and without modifications, you must retain
- this notice and the following text and disclaimers in all such redistributions of
- the Apple Software. Neither the name, trademarks, service marks or logos of
- Apple Computer, Inc. may be used to endorse or promote products derived from the
- Apple Software without specific prior written permission from Apple. Except as
- expressly stated in this notice, no other rights or licenses, express or implied,
- are granted by Apple herein, including but not limited to any patent rights that
- may be infringed by your derivative works or by other works in which the Apple
- Software may be incorporated.
-
- The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
- WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
- WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
- COMBINATION WITH YOUR PRODUCTS.
-
- IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
- GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
- OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
- (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
- ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-
- // system includes ----------------------------------------------------------
-
- #include <Events.h>
- #include <Fonts.h>
- #include <Windows.h>
- #include <Dialogs.h>
- #include <ToolUtils.h>
- #include <Devices.h>
-
- #include <DrawSprocket.h>
-
- #include <stdio.h>
-
- // project includes ---------------------------------------------------------
-
-
- // function declarations ----------------------------------------------------
-
- void InitToolbox(void);
- void GraphicsInitAttributes(DSpContextAttributes *inAttributes);
- Boolean SetUp (void);
- void DoMenu (SInt32 menuResult);
- void SwitchDSpContext(short context);
- void DoKey (SInt8 theKey, SInt8 theCode);
- void CToPStr (register const char *inString, Str255 * outString );
- void DoUpdate (void);
- void DoEvent (void);
- void CleanUp (void);
-
- // statics/globals (internal only) ------------------------------------------
-
- // Menu defs
- enum
- {
- kNumContexts = 8,
- kBitsPerPixel = 16,
-
- kMenuApple = 128,
- kMenuFile = 129,
-
- kAppleAbout = 1,
- kFileDoNothing = 1,
- kFileQuit
- };
-
- Boolean gDone = false;
- UInt32 gSleepTime = 1;
-
- RGBColor grgbColor = {0x5000,0x5000,0x5000};
-
- short gContextCurrent = 0;
- short gActualNumContexts = 0;
- Point gapntResolution [kNumContexts] = {{480, 640}, {600, 800}, {624, 832}, {768, 1024}, {870, 1152}, {1024, 1280}, {1600, 1200}, {1920, 1440}};
- DSpContextAttributes gaContextAttributes [kNumContexts];
- DSpContextReference gaContextRef [kNumContexts];
- DSpContextReference * gpContextRefUnused = NULL;
- unsigned long gDisplayID;
- GDHandle ghGD;
- short gnumDevices = 0;
- Boolean gfDSp17 = true;
-
-
-
- // functions (internal/private) ---------------------------------------------
-
- void InitToolbox (void)
- {
- MenuHandle menu;
- SInt16 modifiers = 0;
- EventRecord event;
-
- MaxApplZone ();
-
- InitGraf((Ptr) &qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
-
- qd.randSeed = (long) TickCount();
-
- // init events
- EventAvail(everyEvent, &event);
- modifiers |= event.modifiers;
- EventAvail(everyEvent, &event);
- modifiers |= event.modifiers;
- EventAvail(everyEvent, &event);
- modifiers |= event.modifiers;
-
- // Init Menus
- menu = NewMenu (kMenuApple, "\p\024"); // new apple menu
- InsertMenu (menu, 0); // add menu to end
- AppendResMenu(menu, 'DRVR');
-
- menu = NewMenu (kMenuFile, "\pFile"); // new menu
- InsertMenu (menu, 0); // add menu to end
- AppendMenu (menu, "\pDo Nothing;Quit/Q"); // add items
-
- DrawMenuBar();
- }
-
- // --------------------------------------------------------------------------
-
- void GraphicsInitAttributes(DSpContextAttributes *inAttributes)
- {
- BlockZero (inAttributes, sizeof (DSpContextAttributes));
- }
-
- // --------------------------------------------------------------------------
-
- Boolean SetUp (void)
- {
- OSStatus err = noErr;
- short i, context;
-
- InitToolbox ();
-
- // Do DrawSprocket Setup
-
- if ((Ptr) kUnresolvedCFragSymbolAddress == (Ptr) DSpStartup)
- {
- DebugStr("\pDSp extension not loaded\n");
- return false;
- }
- if (DSpStartup () != noErr)
- {
- DebugStr("\pUnable to startup DSp\n");
- return false;
- }
- if ((Ptr) kUnresolvedCFragSymbolAddress == (Ptr) DSpGetVersion)
- gfDSp17 = false;
- else
- {
- // do not use built in DSpContext_Reserve and DSpContext_Queue prior to DSp 1.7.3
- NumVersion version = DSpGetVersion ();
- if ((version.majorRev < 0x01) || ((version.majorRev == 0x01) && (version.minorAndBugRev < 0x73)))
- gfDSp17 = false;
- }
-
- context = 0;
- for (i = 0; i < kNumContexts; i++) // get all of our possible contexts kNumContexts
- {
- GraphicsInitAttributes (&gaContextAttributes [context]);
-
- gaContextAttributes [context].displayWidth = gapntResolution [i].h;
- gaContextAttributes [context].displayHeight = gapntResolution [i].v;
- gaContextAttributes [context].colorNeeds = kDSpColorNeeds_Require;
- gaContextAttributes [context].displayBestDepth = kBitsPerPixel;
- gaContextAttributes [context].displayDepthMask = kDSpDepthMask_All;
- // this is required prior to DSp 1.7.3, even if you only need one buffer
- gaContextAttributes [context].backBufferBestDepth = kBitsPerPixel;
- gaContextAttributes [context].backBufferDepthMask = kDSpDepthMask_All;
- gaContextAttributes [context].pageCount = 1; // only the front buffer is needed
- // use two above if you want to swap buffers to animate
-
- if (0 == context) // for the first context
- {
- // will display user dialog if context selection is required otherwise as find best context
- err = DSpUserSelectContext(&gaContextAttributes [context], 0, nil, &gaContextRef[context]);
- if (err != noErr)
- {
- // DebugStr("\pA suitable display context could not be found.");
- }
-
- if (noErr != DSpContext_GetAttributes (gaContextRef[context], &gaContextAttributes [context])) // see what we actually found
- {
- DebugStr("\pUnable to get attributes\n");
- }
- gaContextAttributes [context].displayWidth = gapntResolution [i].h;
- gaContextAttributes [context].displayHeight = gapntResolution [i].v;
- gaContextAttributes [context].pageCount = 1; // only the front buffer is needed
- gaContextAttributes [context].contextOptions = 0 | kDSpContextOption_DontSyncVBL; // no page flipping and no VBL sync needed
-
- // get our device for future use
- err = DSpContext_GetDisplayID (gaContextRef [context], &gDisplayID);
- if (err != noErr)
- {
- DebugStr("\pDSpContext_GetDisplayID () had an error.");
- return false;
- }
-
- // get GDHandle for ID'd device
- err = DMGetGDeviceByDisplayID (gDisplayID, &ghGD, false);
- if (err != noErr)
- {
- DebugStr("\pDMGetGDeviceByDisplayID() had an error.");
- return false;
- }
-
- if (false == gfDSp17) // reserve contexts on other screens to prevent selection
- {
- GDHandle hDevice = DMGetFirstScreenDevice (true); // check number of screens
- short indexDevice = 0;
- do
- {
- gnumDevices++;
- hDevice = DMGetNextScreenDevice (hDevice, true);
- }
- while (hDevice);
- gnumDevices--; // only count unsued screens
- if (gnumDevices)
- {
- gpContextRefUnused = (DSpContextReference *) NewPtr ((long) sizeof (DSpContextReference) * gnumDevices);
- hDevice = DMGetFirstScreenDevice (true); // check number of screens
- do
- {
- if (hDevice != ghGD) // if this device is not the one the user chose
- {
- unsigned long displayID;
- DSpContextAttributes contextAttributes;
- err = DMGetDisplayIDByGDevice (hDevice, &displayID, false);
- err = DSpGetFirstContext (displayID, &gpContextRefUnused [indexDevice]); // get a context and
- err = DSpContext_GetAttributes (gpContextRefUnused [indexDevice], &contextAttributes); // find attributes
- err = DSpContext_Reserve (gpContextRefUnused [indexDevice], &contextAttributes); // reserve it
- indexDevice++;
- }
- hDevice = DMGetNextScreenDevice (hDevice, true);
- }
- while (hDevice);
- }
- }
- }
- else
- {
- // find a context on the display used in the first context
- if (false == gfDSp17)
- err = DSpFindBestContext (&gaContextAttributes [context], &gaContextRef [context]);
- else // in this case we have reserved contexts on all other screens thus they will not be considered
- err = DSpFindBestContextOnDisplayID (&gaContextAttributes [context], &gaContextRef [context], gDisplayID);
- if (noErr == err)
- {
- if (noErr != DSpContext_GetAttributes (gaContextRef[context], &gaContextAttributes [context])) // see what we actually found
- {
- DebugStr("\pUnable to get attributes\n");
- }
- gaContextAttributes [context].displayWidth = gapntResolution [context].h;
- gaContextAttributes [context].displayHeight = gapntResolution [context].v;
- gaContextAttributes [context].pageCount = 1; // only the front buffer is needed
- gaContextAttributes [context].contextOptions = 0 | kDSpContextOption_DontSyncVBL; // no page flipping and no VBL sync needed
- }
- else
- {
- // DebugStr( "\pDSpFindBestContextOnDisplayID could not find context");
- }
- }
- if (gaContextRef[context]) // if we use that slot
- {
- context++;
- gActualNumContexts++;
- }
- }
- gContextCurrent = 0; // start at the first
-
- // reserve our context
- err = DSpContext_Reserve (gaContextRef [gContextCurrent], &gaContextAttributes [gContextCurrent]);
- if (err != noErr)
- {
- DebugStr("\pUnable to create the display!");
- return false;
- }
-
- if (false == gfDSp17)
- {
- // do nothing
- }
- else
- {
- for (i = 1; i < gActualNumContexts; i++) // get all of our contexts
- {
- // queue it for switch
- err = DSpContext_Queue(gaContextRef [gContextCurrent], gaContextRef [i], &gaContextAttributes [i]);
- if (err)
- DebugStr( "\pDSpContext_Queue() had an error.");
- }
- }
-
- DSpSetBlankingColor(&grgbColor);
- HideCursor();
-
- err = DSpContext_FadeGammaOut (NULL, NULL); // remove for debug
- if (err != noErr)
- DebugStr("\pUnable to fade the display!");
-
- // activate our context
- err = DSpContext_SetState (gaContextRef [gContextCurrent], kDSpContextState_Active);
- if (err != noErr)
- {
- DebugStr("\pUnable to set the display!");
- return false;
- }
-
- err = DSpContext_FadeGammaIn (NULL, NULL);
- if(err)
- DebugStr("\pUnable to fade the display!");
-
- return true;
- }
-
- // --------------------------------------------------------------------------
-
- void DoMenu (SInt32 menuResult)
- {
- SInt16 theMenu;
- SInt16 theItem;
- Str255 daName;
- MenuRef theMenuHandle;
-
- theMenu = HiWord(menuResult);
- theItem = LoWord(menuResult);
- theMenuHandle = GetMenuHandle(theMenu);
-
- switch (theMenu)
- {
- case kMenuApple:
- switch (theItem)
- {
- case kAppleAbout:
- break;
- default:
- OpenDeskAcc(daName);
- break;
- }
- break;
- case kMenuFile:
- switch (theItem)
- {
- case kFileDoNothing:
- break;
- case kFileQuit:
- gDone = true;
- break;
- }
- break;
- }
- HiliteMenu(0);
- DrawMenuBar();
- }
-
- // --------------------------------------------------------------------------
-
- void SwitchDSpContext(short context)
- {
- OSStatus err;
- if(gContextCurrent != context)
- {
- if(gaContextRef[gContextCurrent] && gaContextRef[context])
- {
- if (false == gfDSp17)
- {
- // fade out
- err = DSpContext_FadeGammaOut (NULL, NULL); // remove for debug
- if (err != noErr)
- DebugStr("\pUnable to fade the display!");
-
- // deactivate our context
- err = DSpContext_SetState (gaContextRef [gContextCurrent], kDSpContextState_Inactive);
- if (err != noErr)
- DebugStr("\pUnable to set the display!");
-
- // release
- err = DSpContext_Release (gaContextRef [0]);
- if (err != noErr)
- DebugStr ("\pDSpContext_Release error");
-
- // reserve
- err = DSpContext_Reserve (gaContextRef [context], &gaContextAttributes [context]);
- if (err != noErr)
- DebugStr("\pUnable to create the display!");
-
- // activate
- err = DSpContext_SetState (gaContextRef [context], kDSpContextState_Active);
- if (err != noErr)
- DebugStr("\pUnable to set the display!");
-
- // fade in
- err = DSpContext_FadeGammaIn (NULL, NULL);
- if(err)
- DebugStr("\pUnable to fade the display!");
-
- if(err)
- DebugStr ("\pUnable to switch displays");
- else
- gContextCurrent = context;
- }
- else
- {
- err = DSpContext_Switch(gaContextRef[gContextCurrent], gaContextRef[context]);
- if(err)
- DebugStr ("\pUnable to switch displays");
- else
- gContextCurrent = context;
- }
- }
- }
- }
-
- // --------------------------------------------------------------------------
-
- void DoKey (SInt8 theKey, SInt8 theCode)
- {
- #pragma unused (theCode)
- short context = gContextCurrent;
- if (theKey == '\35')
- {
-
- if (++context == gActualNumContexts)
- context = 0;
- SwitchDSpContext (context);
- DoUpdate ();
- }
- else if (theKey == '\34')
- {
- if (--context < 0)
- context = (short) (gActualNumContexts - 1);
- SwitchDSpContext (context);
- DoUpdate ();
- }
- }
-
- //---------------------------------------------------------------------------
-
- // Copy C string to Pascal string
-
- void CToPStr (register const char *inString, Str255 * outString ) // copies in to out
- {
- register unsigned char x = 0;
- do
- {
- *(((char*)outString) + 1 + x) = *(inString + x);
- x++;
- }
- while ((*(inString + x) != 0) && (x < 256));
- *((char*)outString) = (char) x;
- }
-
- // --------------------------------------------------------------------------
-
- void DoUpdate (void)
- {
- OSStatus err;
- CGrafPtr pCGrafFrontBuffer, pCGrafSave = NULL;
- GDHandle hGDSave;
- RGBColor rgbSave, rgbRed = {0x7FFF, 0x0000, 0x0000}, rgbWhite = {0xFFFF, 0xFFFF, 0xFFFF};
- short fNum, len;
- Str255 aStr = "\p";
- char aChar[256];
-
- err = DSpContext_GetFrontBuffer (gaContextRef [gContextCurrent], &pCGrafFrontBuffer);
- if (err != noErr)
- {
- DebugStr("\pUnable to get front buffer");
- return;
- }
-
- GetGWorld (&pCGrafSave, &hGDSave);
- SetGWorld (pCGrafFrontBuffer, ghGD);
- GetForeColor (&rgbSave);
-
- // draw here
- RGBForeColor (&rgbRed);
- PaintRect (&pCGrafFrontBuffer->portRect);
-
- RGBForeColor (&rgbWhite);
-
- // set text
- GetFNum("\pGeneva", &fNum);
- TextFont(fNum);
- TextSize(12);
-
- // draw centered to screen
- sprintf (aChar, "%d x %d", gapntResolution [gContextCurrent].h, gapntResolution [gContextCurrent].v);
- CToPStr (aChar, &aStr);
- len = StringWidth (aStr);
- MoveTo ((short) (pCGrafFrontBuffer->portRect.left + (pCGrafFrontBuffer->portRect.right - pCGrafFrontBuffer->portRect.left) / 2 - (len / 2)), 18);
- DrawString(aStr);
-
- RGBForeColor (&rgbSave);
- SetGWorld (pCGrafSave, hGDSave);
- }
-
- // --------------------------------------------------------------------------
-
- void DoEvent (void)
- {
- EventRecord theEvent;
- SInt16 whatPart;
- SInt32 menuResult;
- WindowRef whichWindow;
- SInt8 theKey;
- SInt8 theCode;
- Boolean fProcessed;
-
- if (WaitNextEvent(everyEvent, &theEvent, gSleepTime, NULL))
- {
- DSpProcessEvent (&theEvent, &fProcessed);
- if (!fProcessed)
- {
- switch (theEvent.what)
- {
- case mouseDown:
- whatPart = FindWindow(theEvent.where, &whichWindow);
- switch (whatPart)
- {
- case inGoAway:
- break;
- case inMenuBar:
- DrawMenuBar();
- menuResult = MenuSelect(theEvent.where);
- if (HiWord(menuResult) != 0)
- DoMenu(menuResult);
- break;
- case inSysWindow:
- SystemClick(&theEvent, whichWindow);
- break;
- case inContent:
- break;
- }
- break;
- case keyDown:
- case autoKey:
- theKey = (char)(theEvent.message & charCodeMask);
- theCode = (char)((theEvent.message & keyCodeMask) >> 8);
- if ((theEvent.modifiers & cmdKey) != 0)
- {
- menuResult = MenuKey(theKey);
- if (HiWord(menuResult) != 0)
- DoMenu (menuResult);
- }
- else
- DoKey (theKey, theCode);
- break;
- case updateEvt:
- BeginUpdate((WindowRef) theEvent.message);
- DoUpdate();
- EndUpdate((WindowRef) theEvent.message);
- break;
- case diskEvt:
- break;
- case osEvt:
- if (theEvent.message & 0x01000000) // Suspend/resume event
- {
- if (theEvent.message & 0x00000001) // Resume
- {
- gSleepTime = 1; // Suspend
- DoUpdate ();
- }
- else
- gSleepTime = 32000; // Suspend
- }
- break;
-
- case kHighLevelEvent:
- AEProcessAppleEvent(&theEvent);
- break;
- }
- }
- }
- else
- {
- // idle tasks
- }
- }
-
- // --------------------------------------------------------------------------
-
- void CleanUp (void)
- {
- OSStatus err;
-
- err = DSpContext_FadeGammaOut (NULL, NULL); // remove for debug
- if (err != noErr)
- DebugStr("\pUnable to fade the display!");
-
- err = DSpContext_SetState (gaContextRef [gContextCurrent], kDSpContextState_Inactive);
- if (err != noErr)
- DebugStr("\pUnable to set the display!");
-
- err = DSpContext_FadeGammaIn (NULL, NULL);
- if(err)
- DebugStr("\pUnable to fade the display!");
-
- if (false == gfDSp17)
- {
- err = DSpContext_Release (gaContextRef [gContextCurrent]);
- if (err != noErr)
- DebugStr ("\pDSpContext_Release error");
-
- while (gnumDevices--) // dump our reserved unused devices
- {
- err = DSpContext_Release (gpContextRefUnused [gnumDevices]);
- if (err != noErr)
- DebugStr ("\pDSpContext_Release error");
- }
- }
- else
- {
- err = DSpContext_Release (gaContextRef [0]);
- if (err != noErr)
- DebugStr ("\pDSpContext_Release error");
- }
-
- err = DSpShutdown ();
- if (err != noErr)
- DebugStr ("\pDSpShutdown error");
- }
-
- // --------------------------------------------------------------------------
-
- int main (void)
- {
- if (SetUp ())
- {
- DoUpdate ();
- while (!gDone)
- {
- DoEvent ();
- }
- }
- CleanUp ();
- return 0;
- }
-
-
-